# coding=utf-8

'''
Created on 2011-8-28

@author: chris
'''

import sys
sys.path.append('/harvester/ripper/source/src/')

import pymongo #@UnresolvedImport
from pymongo import Connection #@UnresolvedImport
import datetime
from ripper.core import RequestDispatcher
from ripper.parser.Parser import Parser
import re

def mongo_test():
    conn = Connection()
    db = conn['harvester']
    projects = db['projects']
    prj = {
           'name' : 'TTDB_',
           'url' :'http://www.tabletennisdb.com/blade/',
           'time' : datetime.datetime.utcnow(), 
           'says':  u'灭哈哈',
           'prop' : ['bladeName', {'speed' : 655}],
           }
    prj_new = projects.insert(prj)
    #print projects.find_one(prj_new)['says']
    for p in projects.find({'name' : 'TTDB_'}).sort('time', pymongo.DESCENDING):
            print p['prop'][1]['speed']
    
def updateCache():
    RequestDispatcher.make_and_start_thread_pool(number_of_threads_in_pool=30)
    p = Parser(None, False)
    p.login_mySite()
    pageNum = 1000
    for i in range(1, pageNum+1):
        url = 'http://h.pphime.com/z/mlist/' + str(i)
        RequestDispatcher.request_work( (p.get_content, [url]) )
        
    RequestDispatcher.stop_and_free_thread_pool()    
    
def testRe():
    text = open('h:/resource/euro_torrent.htm').read()
    for m in re.findall(r'(www1.*?hopefind.*?ref=[a-z,A-Z,0-9]{10})',text):
            print m
    
    
if __name__ == '__main__':
    testRe()   